package com.yl.ydk.presenter.impl

import android.content.Context
import android.content.Intent
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.Base64UploadBean
import com.yl.ydk.bean.SaveUserBean
import com.yl.ydk.presenter.contract.CompletedUserInfoActivityContract
import com.yl.ydk.ui.activity.HomeActivity
import com.yl.ydk.utils.GlideUtils
import okhttp3.MultipartBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class CompletedUserInfoActivityImpl : BasePresenter<CompletedUserInfoActivityContract.View>(),
    CompletedUserInfoActivityContract.Presenter<CompletedUserInfoActivityContract.View> {


    override fun getBase64Upload(context: Context, requestBody: MultipartBody) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getBase64Upload(requestBody)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<Base64UploadBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: Base64UploadBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {
                            GlideUtils.loadRound(context, t.data[0], mView?.getIvHead())
                            mView?.getPhoto(t.data[0])
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

    override fun getSaveUser(context: Context, vararg query: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getSaveUser(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<SaveUserBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: SaveUserBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {
                            mView?.getActivity()?.let {
                                it.startActivityIn(Intent(it, HomeActivity::class.java), it)
                                it.finish()
                            }
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

}